<template>
	<div class="app-container">
		<div class="filter-container">
			<div class="fr">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加</el-button>
      <!-- <el-button class="filter-item" style="margin-left: 10px;" @click="handleDelete" type="danger" icon="el-icon-delete">删除</el-button> -->
      </div>
    </div>
    <tree-table :data="list" :evalFunc="func" :evalArgs="args" :expandAll="expandAll" border>
      <el-table-column label="名称" prop="name">
      </el-table-column>
      <el-table-column label="路径" show-overflow-tooltip prop="url">
      </el-table-column>
			<el-table-column label="图标" show-overflow-tooltip prop="icon">
        <template slot-scope="scope">
          {{scope.row.icon}}<svg-icon v-if="scope.row.icon!=null&&scope.row.icon!=''" :icon-class="scope.row.icon"></svg-icon>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
        	<el-button size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="550px">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="80px" style='width: 100%; '>
        <el-form-item label="上级" prop="parentId">
          <treeselect v-model="temp.parentId" :options="seleteMenuList" :normalizer='normalizer' :props="listOp"/>
        </el-form-item>
        <el-form-item label="名称" prop="menuName">
          <el-input v-model="temp.menuName"></el-input>
        </el-form-item>
				<el-form-item label="排序" prop="orderNum">
          <el-input v-model="temp.orderNum"></el-input>
        </el-form-item>
        <el-form-item prop="url" label="路径" :rules="this.temp.menuType==1?rules.url:[{ required: false, message: '请填写审核备注', trigger: 'blur' }]">
					<el-input v-model="temp.url"></el-input>
        </el-form-item>
        <el-form-item prop="component" label="组件">
					<el-input v-model="temp.component"></el-input>
        </el-form-item>
				<el-form-item prop="icon" label="图标">
					<el-input v-model="temp.icon" style="width:150px"></el-input><svg-icon v-if="temp.icon!=null&&temp.icon!=''" :icon-class="temp.icon"></svg-icon>
        </el-form-item>
				<el-form-item prop="menuType" label="类型">
					<el-radio-group v-model="temp.menuType">
            <el-radio :label="0">目录</el-radio>
						<el-radio :label="1">菜单</el-radio>
						<el-radio :label="2">按钮</el-radio>
					</el-radio-group>
        </el-form-item>
				<el-form-item prop="status" label="状态">
					<el-radio-group v-model="temp.status">
						<el-radio :label="0">正常</el-radio>
						<el-radio :label="1">停用</el-radio>
					</el-radio-group>
        </el-form-item>
				<el-form-item prop="perms" label="标识">
					<el-input v-model="temp.perms"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
	</div>
</template>
<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { saveMenu, updateMenu, getMenuInfo, getMenuList, deleteMenu, onlyGetMenuList } from '@/api/menu'
import waves from '@/directive/waves' // 水波纹指令
import { showMsg } from '@/utils'

import treeTable from '@/components/TreeTable'
import treeToArray from '../table/treeTable/customEval'
export default {
  name: 'complexTable',
  directives: {
    waves
  },
  components: { Treeselect, treeTable },
  data() {
    return {
      func: treeToArray,
      expandAll: true,
      seleteMenuList: null,

      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      tableKey: 0,
      list: [],
      total: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 15
      },
      temp: {
        id: undefined,
        parentId: null,
        menuName: null,
        orderNum: 0,
        menuType: 1,
        url: null,
        icon: null,
        status: 0,
        component: null,
        perms: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        menuName: [{ required: true, message: '必填', trigger: 'blur' }],
        url: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      listOp: {
        value: 'id',
        label: 'name'
      },
      downloadLoading: false,
      args: [null, null, 'menuName']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getMenuList(this.listQuery).then(response => {
        this.list = response.data.data
        this.clearNull(this.list)
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getSeleteMenu() {
      if (this.seleteMenuList === null) {
        onlyGetMenuList().then(response => {
          this.seleteMenuList = response.data.data
          this.clearNull(this.seleteMenuList)
        })
      }
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete(row) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var MenuIds = [row.id]
        deleteMenu(MenuIds).then(res => {
          if (res.data.code === 0) {
            showMsg(res.data.message, 1)
            this.getList()
          } else {
            showMsg(res.data.message, 2)
          }
        })
      }).catch(() => {
        // console.log('===')
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        menuName: null,
        orderNum: 0,
        parentId: null,
        menuType: 1,
        url: null,
        icon: null,
        status: 0,
        perms: null
      }
    },
    handleCreate() {
      this.resetTemp()
      this.getSeleteMenu()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveMenu(this.temp).then(res => {
            if (res.data.code === 0) {
              this.dialogFormVisible = false
              showMsg(res.data.message, 1)
              this.getList()
            } else {
              showMsg(res.data.message, 2)
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.getSeleteMenu()
      getMenuInfo(row.id).then(response => {
        this.temp = Object.assign({}, response.data.data) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        if (this.temp.parentId === 0) {
          this.temp.parentId = null
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.temp.parentId || this.temp.parentId === null) {
            this.temp.parentId = 0
          }
          const tempData = Object.assign({}, this.temp)
          updateMenu(tempData).then(res => {
            if (res.data.code === 0) {
              this.dialogFormVisible = false
              showMsg(res.data.message, 1)
              this.getList()
            } else {
              showMsg(res.data.message, 2)
            }
          })
        }
      })
    },
    clearNull(arr) {
      for (var i in arr) {
        if (arr[i].children.length === 0) {
          console.log('-------', arr[i].id)
          delete arr[i].children
        } else {
          this.clearNull(arr[i].children)
        }
      }
      console.log('0', arr)
    }
    // handleDelete(row) {
    //   this.$prompt('请输入密码', '确认删除？', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消'
    //     // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
    //     // inputErrorMessage: '邮箱格式不正确'
    //   }).then(({ value }) => {
    //     this.$message({
    //       type: 'success',
    //       message: '你的密码是: ' + value
    //     })
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '取消输入'
    //     })
    //   })
    // }
  }
}
</script>